"""TAPA fast cosim"""

# Copyright (c) 2024 RapidStream Design Automation, Inc. and contributors.
# All rights reserved. The contributor(s) of this file has/have agreed to the
# RapidStream Contributor License Agreement.

load("@rules_pkg//pkg:mappings.bzl", "pkg_attributes", "pkg_filegroup", "pkg_files", "pkg_mklink", "strip_prefix")
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_python//python:py_binary.bzl", "py_binary")
load("//bazel:nuitka_rules.bzl", "nuitka_binary")

nuitka_binary(
    name = "nuitka",
    src = ":tapa-fast-cosim",
    flags = [
        "--deployment",
        "--include-package-data=tapa:VERSION",
        "--python-flag=isolated,no_docstrings,no_site",
    ],
    output_name = "tapa-fast-cosim",
)

py_binary(
    name = "tapa-fast-cosim",
    srcs = ["__main__.py"],
    data = [
        "//fpga-runtime:tapa_fast_cosim_dpi_legacy_rdi",
        "//fpga-runtime:tapa_fast_cosim_dpi_xv",
    ],
    main = "__main__.py",
    visibility = ["//visibility:public"],
    deps = [
        ":tapa-cosim-py-libs",
        "//tapa:tapa-py-libs",
    ],
)

py_library(
    name = "tapa-cosim-py-libs",
    srcs = [
        ":common",
        ":config_preprocess",
        ":templates",
        ":vivado",
    ],
)

py_library(
    name = "common",
    srcs = ["common.py"],
)

py_library(
    name = "config_preprocess",
    srcs = ["config_preprocess.py"],
)

py_library(
    name = "templates",
    srcs = ["templates.py"],
    deps = [":common"],
)

py_library(
    name = "vivado",
    srcs = ["vivado.py"],
)

pkg_files(
    name = "pkg-runtime",
    srcs = [":nuitka"],
    attributes = pkg_attributes(mode = "0755"),
    prefix = "usr/share/tapa/cosim-runtime",
    strip_prefix = strip_prefix.from_pkg("__main__.dist"),
    visibility = ["//visibility:public"],
)

# link usr/share/tapa/cosim-runtime/tapa-fast-cosim to /usr/bin/tapa-fast-cosim
pkg_mklink(
    name = "pkg-link",
    link_name = "usr/bin/tapa-fast-cosim",
    target = "../share/tapa/cosim-runtime/tapa-fast-cosim",
    visibility = ["//visibility:public"],
)

pkg_filegroup(
    name = "pkg",
    srcs = [
        ":pkg-link",
        ":pkg-runtime",
    ],
    visibility = ["//visibility:public"],
)
